package com.example.firstapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;

import java.util.Objects;

public class Test03BakActivity extends AppCompatActivity {
    TextView tv_tianqi;
    TextView tv_wendu;
    TextView tv_fengxiang;
    TextView tv_fengli;
    ImageView img;
    WeatherNowBean.NowBaseBean now;

    @SuppressLint({"MissingInflatedId", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test03_bak);

        tv_tianqi = (TextView) findViewById(R.id.tv_tianqi);
        tv_wendu = (TextView) findViewById(R.id.tv_wendu);
        tv_fengxiang = (TextView) findViewById(R.id.tv_fengxiang);
        tv_fengli = (TextView) findViewById(R.id.tv_fengli);
        img = findViewById(R.id.img);

        HeConfig.init("HE2210281047491496", "aa13cd38126249b9b54e69dd26a23c24");
        HeConfig.switchToDevService();
        getWeather();
    }

    public void getWeather() {
        /*
         * 实况天气数据
         * @param location 所查询的地区，可通过该地区ID、经纬度进行查询经纬度格式：经度,纬度
         *                 （英文,分隔，十进制格式，北纬东经为正，南纬西经为负)
         * @param lang     (选填)多语言，可以不使用该参数，默认为简体中文
         * @param unit     (选填)单位选择，公制（m）或英制（i），默认为公制单位
         * @param listener 网络访问结果回调
         */

        QWeather.getWeatherNow(Test03BakActivity.this, "CN101110101", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
            @Override
            public void onError(Throwable e) {
                Log.i("hefeng", "getWeather onError: " + e);
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(WeatherNowBean weatherBean) {
                Log.i("hefeng", "getWeather onSuccess: " + new Gson().toJson(weatherBean));
                //先判断返回的status是否正确，当status正确时获取数据，若status不正确，可查看status对应的Code值找到原因
                if (Code.OK == weatherBean.getCode()) {
                    now = weatherBean.getNow();
                    runOnUiThread(() -> setView(now));
                } else {
                    //在此查看返回数据失败的原因
                    Code code = weatherBean.getCode();
                    Log.i("hefeng", "failed code: " + code);
                }
            }
        });

    }

    @SuppressLint("SetTextI18n")
    public void setView(WeatherNowBean.NowBaseBean now) {
        String tianqi = now.getText();
        String wendu = now.getTemp() + "℃";
        String fengli = now.getWindScale();
        String fengxiang = now.getWindDir();
        tv_tianqi.setText("当前天气:" + tianqi);
        tv_wendu.setText("当前温度:" + wendu);
        tv_fengxiang.setText("风向：" + fengxiang);
        tv_fengli.setText("风力：" + fengli + "级");
        if (Objects.equals(tianqi, "晴"))
            img.setImageResource(R.drawable.sun);
        else if (Objects.equals(tianqi, "多云"))
            img.setImageResource(R.drawable.clouds);
        else if (Objects.equals(tianqi, "小雨"))
            img.setImageResource(R.drawable.sprinkle);
    }
}
